package com.zjd.association.controller;

import com.zjd.association.common.PageWrapper;
import com.zjd.association.common.R;
import com.zjd.association.entity.PayLogs;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.PayLogsService;
import com.zjd.association.service.UsersService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 缴费记录 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/payLogs")
public class PayLogsController {

    private final CacheHandle cacheHandle;

    private final UsersService usersService;

    private final PayLogsService payLogsService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/PayLogs";
    }

    /**
     * 获取缴费记录
     *
     * @param id 缴费记录id
     * @return 缴费记录
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        PayLogs payLogs = payLogsService.getOne(id);
        return R.ok(payLogs);
    }

    /**
     * 获取缴费记录
     *
     * @param pageIndex 分页页数
     * @param pageSize  分页大小
     * @param token     用户token
     * @param teamName  活动名称
     * @param userName  用户名
     * @return
     */
    @GetMapping("/page")
    public R getPageInfos(
            Long pageIndex, Long pageSize,
            String token, String teamName, String userName
    ) {
        //获取用户信息
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (user.getType() == 0) {
            //管理员查询全部
            PageWrapper page = payLogsService.getPageInfo(pageIndex, pageSize, null, teamName, userName);
            return R.ok(page);
        } else if (user.getType() == 1) {
            PageWrapper page = payLogsService.getManPageInfo(pageIndex, pageSize, user.getId(), teamName, userName);
            return R.ok(page);
        } else {
            PageWrapper page = payLogsService.getPageInfo(pageIndex, pageSize, user.getId(), teamName, null);
            return R.ok(page);
        }
    }

    /**
     * 添加缴费记录
     *
     * @param payLogs 缴费记录
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(PayLogs payLogs) {
        payLogs.setId(UUIDUtils.makeIDByCurrent());
        payLogs.setCreateTime(DateUtils.getNowDate());
        payLogsService.add(payLogs);
        return R.ok();
    }

    /**
     * 更新缴费记录
     *
     * @param payLogs 缴费记录
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(PayLogs payLogs) {
        payLogsService.update(payLogs);
        return R.ok();
    }

    /**
     * 删除缴费记录
     *
     * @param id 记录id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        PayLogs payLogs = payLogsService.getOne(id);
        payLogsService.delete(payLogs);
        return R.ok();
    }

}